home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_010 / ls / utilskel.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  127 lines

  1. /*
  2. ** The following is a suggested skeleton for utilities which expect
  3. ** to handle a variable number of files with wildcards handled in
  4. ** input files and redirection of output files.  This type of application
  5. ** is very common and is difficult to handle...thus this example.
  6. **
  7. ** The command line syntax for the resulting program is:
  8. **  **name** [-o outputpath] file file file...
  9. **    where the -o parameter is optional and indicates the path to
  10. **    the directory where output files are to be placed.
  11. **
  12. ** The skeleton handles wild card file names in the following fashion:
  13. ** First, if a "-o" option is detected, the skeleton builds an output
  14. ** file name mask consisting of the path given by the user after the
  15. ** option.  Then it reads file names from the command line and uses
  16. ** the "wildexp" function to expand them into a table of existing
  17. ** filenames which match.  For each filename in the table, the skeleton
  18. ** opens a file.  If a "-o" option was given by the user, it concatenates
  19. ** the input filename to the path obtained above, otherwise it
  20. ** concatenates ".f" to the end of the input filename.  These actions
  21. ** are necessary to ensure that the input file and the output file
  22. ** have different names.  The skeleton then opens the output file
  23. ** and calls "dowork".  If there are errors in opening either the
  24. ** input file or the output file, "dowork" is not called and an
  25. ** appropriate message is printed to stderr.
  26. **
  27. ** Basically, to use this skeleton, first replace occurances of "**name**"
  28. ** with the name of the utility you are creating.  Then, write the
  29. ** "dowork" function.  The "dowork" function gets two arguments..."fin"
  30. ** and "fout".  For each file the user wants to process, the "main"
  31. ** function opens an input file (fin) and an output file (fout) and
  32. ** then calls "dowork"...which performs whatever processing there is
  33. ** to be done and then returns.
  34. **
  35. ** Written by:  Rick Schaeffer
  36. **              E. 13611 26th Ave.
  37. **              Spokane, Wa.  99216
  38. **              Compuserve ID: 70120,174
  39. **              Bytenet ID: ricks.
  40. */
  41.  
  42. #include <stdio.h>
  43. extern int Enable_Abort;
  44.  
  45. char    *largv[128];
  46.  
  47. main(argc,argv)
  48. int     argc;
  49. char    *argv[];
  50. {
  51.     char    outpath[80];
  52.     char    outname[80];
  53.     char    *nameonly,*strrchr();
  54.     char    *malloc();
  55.     int     i,j,begarg;
  56.     FILE    *fin,*fout;
  57.  
  58.     Enable_Abort = 1;
  59.     outpath[0] = 0;
  60.     if ((argc < 2) || (strcmp(argv[1],"?") == 0)) {
  61.         printf("**name** -- Usage:\n");
  62.         printf("  **name** [-o outpath] file file\n\n");
  63.         printf("if -o option present, will write files to that path\n");
  64.         printf("Otherwise concatenates '.f' to input file names\n\n");
  65.         exit(1);
  66.         }
  67.     if (strcmp(argv[1],"-o") == 0) {
  68.         strncpy(outpath,argv[2],78);
  69.         begarg = 3;
  70.         }
  71.     else
  72.         begarg = 1;
  73.     for (i=begarg; i<argc; i++) {
  74.         if (iswild(argv[i])) {
  75.             wildexp(argv[i],largv,127);
  76.             if (largv[0] == NULL)
  77.                 fprintf(stderr,"No matching files for ==> %s!\n",argv[i]);
  78.             }
  79.         else {
  80.             largv[0] = malloc(strlen(argv[i])+1);
  81.             strcpy(largv[0],argv[i]);
  82.             largv[1] = NULL;
  83.             }
  84.         j=0;
  85.         while (largv[j] != NULL) {
  86.             if (outpath[0] == 0) {
  87.                 strncpy(outname,largv[j],77);
  88.                 strcat(outname,".f");
  89.                 }
  90.             else {
  91.                 strcpy(outname,outpath);
  92.                 nameonly = strrchr(largv[j],'/');
  93.                 if (nameonly == NULL)
  94.                     nameonly = strrchr(largv[j],':');
  95.                 if (nameonly == NULL)
  96.                     nameonly = largv[j];
  97.                 else
  98.                     nameonly++;
  99.                 if ((strlen(outname) + strlen(nameonly)) > 79) {
  100.                     fprintf(stderr,"Pathname+Filename too long\n");
  101.                     break;
  102.                     }
  103.                 strcat(outname,nameonly);
  104.                 }
  105.             if ((fin = fopen(largv[j],"r")) == NULL) {
  106.                 fprintf(stderr,"Couldn't open input file ==> %s\n",largv[j]);
  107.                 break;
  108.                 }
  109.             if ((fout = fopen(outname,"w")) == NULL) {
  110.                 fprintf(stderr,"Couldn't open output file ==> %s\n",outname);
  111.                 fclose(fin);
  112.                 break;
  113.                 }
  114.             fprintf(stderr,"%s ==> %s\n",largv[j],outname);
  115.             dowork(fin,fout);
  116.             fclose(fin);
  117.             fclose(fout);
  118.             j++;
  119.             }
  120.         }
  121. }
  122.  
  123. dowork(*fin,*fout)
  124. FILE    *fin,*fout;
  125. {
  126. }
  127.